<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">课程平台登陆</div>
            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item style="margin-top: 30px" prop="number">
                    <el-input size="large" prefix-icon="el-icon-s-custom" v-model="param.number" placeholder="请输入您的号码"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input
                        size="large"
                        type="password"
                        prefix-icon="el-icon-lx-lock"
                        placeholder="请输入您的密码"
                        v-model="param.password"
                        @keyup.enter.native="submitForm()"
                    ></el-input>
                </el-form-item>
                <el-form-item>
                    <el-radio-group v-model="param.radio">
                        <el-radio :label="1">我是学生</el-radio>
                        <el-radio :label="2">我是老师</el-radio>
                        <el-radio :label="3">我是管理员</el-radio>
                    </el-radio-group>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm()">登录</el-button>
                </div>
                <!-- <el-button type="primary" @click="textWeb()">测试</el-button> -->
                <p class="login-tips">Tips : 学生用户名和密码默认为学号。</p>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    data: function () {
        return {
            param: {
                number: '',
                password: '',
                radio: 1
            },
            adminLogin: {
                adminId: 0,
                adminPassword: ''
            },
            value: '',
            rules: {
                number: [{ required: true, message: '请输入课题名称', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
                radio: [{ required: true, message: '请选择登陆身份', trigger: 'blur' }]
            }
        };
    },
    methods: {
        submitForm() {
            this.$refs.login.validate((valid) => {
                if (valid) {
                    if (this.param.radio == 1) {
                        this.$http.post('http://localhost:9900/login/loginText', this.param).then((res) => {
                            console.log(res.data);
                            if (res.data != null && res.data != '') {
                                // var aData = new Date();
                                // console.log(aData); //Wed Aug 21 2019 10:00:58 GMT+0800 (中国标准时间)
                                // this.value = aData.getFullYear() + '-' + (aData.getMonth() + 1) + '-' + aData.getDate();
                                // console.log(this.value); //2019-8-20
                                // localStorage.setItem('ms_time', this.value);
                                var role = '学生';
                                this.$message.success('登录成功');
                                localStorage.setItem('ms_role', role);
                                localStorage.setItem('ms_number', res.data.number);
                                //localStorage.setItem('ms_username', res.data.studentName);
                                //console.log(res.data.studentMajor.majorName);
                                //localStorage.setItem('ms_major', res.data.studentMajor.majorName);
                                //localStorage.setItem('ms_class', res.data.studentClass.className);
                                this.$router.push('/');
                            } else {
                                this.$message.error('该账号不存在或密码错误！');
                            }
                        });
                    }
                    if (this.param.radio == 2) {
                        if (this.param.number.length > 10) {
                            this.$message.error('账号输入有误，请重新输入！');
                            this.param.number = '';
                            this.param.password = '';
                        } else {
                            this.param.number = parseInt(this.param.number);
                            console.log(this.param.number);
                            this.$http.post('http://localhost:9900/login/TeacherLogin', this.param).then((res) => {
                                console.log(res.data);
                                if (res.data != null && res.data != '') {
                                    var role = '老师';
                                    this.$message.success('登录成功');
                                    localStorage.setItem('ms_role', role);
                                    localStorage.setItem('ms_number', res.data.number);
                                    console.log(res.data.number);
                                    this.$router.push('/dashboardTeacher');
                                } else {
                                    this.$message.error('该账号不存在或密码错误！');
                                }
                            });
                        }
                    }
                    if (this.param.radio == 3) {
                        this.adminLogin.adminId = parseInt(this.param.number);
                        this.adminLogin.adminPassword = this.param.password;
                        console.log(this.adminLogin);
                        this.$http.post('http://localhost:9900/admin/adminLogin', this.adminLogin).then((res) => {
                            console.log(res.data);
                            if (res.data == '') {
                                this.$message.error('该账号不存在或密码错误！');
                            } else {
                                this.$message.success('登录成功');
                                localStorage.setItem('ms_role', '管理员');
                                localStorage.setItem('ms_number', res.data.adminId);
                                this.$router.push('/addStudent');
                            }
                        });
                    }
                    // console.log(this.param.radio);
                    // this.$message.success('登录成功');
                    // localStorage.setItem('ms_username', this.param.username);
                    // this.$router.push('/');
                } else {
                    this.$message.error('请输入账号和密码');
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        textWeb() {
            var websocket = null;
            if ('WebSocket' in window) {
                websocket = new WebSocket('ws://localhost:9900/webSocket/001');
                console.log('测试链接打开');
                websocket.onopen = function () {
                    console.log('链接打开！');
                    websocket.send('发送测试消息');
                };
                //收到消息事件
                websocket.onmessage = function (msg) {
                    console.log(msg);
                };
                //关闭链接
                websocket.onclose = function () {
                    console.log('链接关闭');
                };
                //发生了错误事件
                websocket.onerror = function () {
                    alert('Socket发生了错误');
                };

                //窗口关闭时，关闭连接
                window.unload = function () {
                    websocket.close();
                };
            }
        }
    }
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/back.png);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 70px;
    text-align: center;
    font-size: 30px;
    color: rgb(27, 27, 27);
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 400px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    /* background: rgba(122, 118, 118, 0.3); */
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 50%;
    height: 36px;
    margin-bottom: 10px;
    background-color: rgb(59, 156, 212);
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: rgb(138, 128, 128);
}
</style>